package com.liaoyi.myble.activity;

import android.bluetooth.BluetoothGattCharacteristic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.liaoyi.myble.App;
import com.liaoyi.myble.R;
import com.liaoyi.myble.base.BaseActivity;
import com.liaoyi.myble.service.BluetoothService;

public class ContentActivity extends BaseActivity implements View.OnClickListener
{
    private static final String TAG = "ContentActivity";
    private BluetoothService bluetoothService;
    private String mac;
    private BleReceiver mBleReceiver;

    private TextView receiveText;
    private EditText sendText;
    private Button sendButton;

    private String message = "";
    public static void actionStart(Context context, String mac) {
        Intent intent = new Intent(context, ContentActivity.class);
        intent.putExtra("mac", mac);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        bluetoothService = App.instance.bluetoothService;
        Intent intent = getIntent();
        mac = intent.getStringExtra("mac");
        registerBleReceiver();
        connectDevice();
        initView();
    }

    private void initView() {
        receiveText = (TextView) findViewById(R.id.receive_text);
        sendText = (EditText) findViewById(R.id.send_edit);
        sendButton = (Button) findViewById(R.id.send_button);
        sendButton.setOnClickListener(this);
    }

    private void connectDevice() {
        bluetoothService.connected(mac);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        bluetoothService.disconnected();
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.send_button:
                send();
                break;
        }
    }

    private void send() {
        BluetoothGattCharacteristic bluetoothGattCharacteristic = App.instance.bluetoothService.mBluetoothGattCharacteristicNotify;
        bluetoothGattCharacteristic.setValue(sendText.getText().toString());
        App.instance.bluetoothService.bluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic );
    }

    private class BleReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action){
                case BluetoothService.ACTION_GATT_CONNECTED:
                    Log.e(TAG, "onReceive: 连接成功"  );
                    break;
                case BluetoothService.ACTION_GATT_DISCONNECTED:
                    Log.e(TAG, "onReceive: 断开连接" );
                    break;
                case BluetoothService.ACTION_CONNECTING_FAIL:
                    Log.e(TAG, "onReceive: 连接失败" );
                    break;
                case BluetoothService.ACTION_DATA_AVAILABLE:
                    byte[] data = intent.getByteArrayExtra(BluetoothService.EXTRA_DATA);
                    // Log.e("蓝牙", "收到的数据：" + new String(data));
                    message += new String(data);
                    receiveText.setText(message);
                    break;
            }
        }
    }

    /**
     * 注册蓝牙信息接收器
     */
    private void registerBleReceiver() {
        // 注册蓝牙信息广播接收器
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothService.ACTION_GATT_CONNECTED);
        filter.addAction(BluetoothService.ACTION_GATT_DISCONNECTED);
        filter.addAction(BluetoothService.ACTION_GATT_SERVICES_DISCOVERED);
        filter.addAction(BluetoothService.ACTION_DATA_AVAILABLE);
        filter.addAction(BluetoothService.ACTION_CONNECTING_FAIL);
        mBleReceiver = new BleReceiver();
        registerReceiver(mBleReceiver, filter);
    }

    @Override
    public void unregisterReceiver(BroadcastReceiver receiver) {
        super.unregisterReceiver(receiver);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBleReceiver);
    }


}
